package jwei.apps.adapters;

import jwei.apps.R;
import jwei.apps.db.StyleCache.StylesCache;
import jwei.apps.helpers.ImageService;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class StylesCursorAdapter extends SimpleCursorAdapter {

    private int layout;

    public StylesCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.layout = layout;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(layout, parent, false);
        return v;
    }

    @Override
    public void bindView(View v, Context context, Cursor c) {
        String productName = c.getString(c.getColumnIndex(StylesCache.PRODUCT_NAME));
        String brandName = c.getString(c.getColumnIndex(StylesCache.BRAND_NAME));
        String imgUrl = c.getString(c.getColumnIndex(StylesCache.IMAGE_URL));

        TextView prodText = (TextView) v.findViewById(R.id.product_name);
        TextView brandText = (TextView) v.findViewById(R.id.brand_name);
        ImageView productImg = (ImageView) v.findViewById(R.id.product_thumbnail);

        Bitmap bm = ImageService.getBitmapFromURL(imgUrl);
        productImg.setImageBitmap(bm);

        prodText.setText(productName);
        brandText.setText(brandName);
    }

}
